package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.admin.mapper.BlogMapper;
import com.itheima.admin.mapper.CategoryMapper;
import com.itheima.admin.service.Blogservice;
import com.itheima.admin.service.CategoryService;
import com.itheima.xo.pojo.Blog;
import com.itheima.xo.pojo.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.admin.service.impl
 * fileName :CategoryServiceImpl
 * creationTime :2022-10-28 16:28
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BlogMapper blogmapper;
    @Override
    public Boolean add(Category category) {

        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Category::getCategoryName,category.getCategoryName());
        Category categoryInDB = categoryMapper.selectOne(lqw);

        if (categoryInDB != null) {
            log.info("分类名“{}”已存在,无法重复创建", category.getCategoryName());
            return false;
        }
        //数值表示操作的行数
        int insert = categoryMapper.insert(category);
        return insert > 0;
    }

    @Override
    public Boolean delete(String id) {

        // 判断分类下是否还有博客绑定着
        LambdaQueryWrapper<Blog> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Blog::getCategoryId,id);
        List<Blog> blogs = blogmapper.selectList(lqw);
        if (blogs.size() > 0) {
            log.info("你输入的ID“{}”下还有绑定着的用户，暂时无法删除", id);
            return false;
        }
        Category categoryInDB = findById(id);
        if (categoryInDB == null) {
            log.info("你输入的ID“{}”不存在，请输入正确的ID", id);
            return false;
        }
        categoryInDB.setDeleted(LocalDateTime.now());
        int update = categoryMapper.updateById(categoryInDB);
        return update>0;
    }

    @Override
    public Boolean update(Category category) {

//        log.info("分类名{}已存在，无法修改",findByName(category.getCategoryName()));

        if(findByName(category.getCategoryName())!=null){
            log.info("分类名{}已存在，无法修改",category.getCategoryName());
            return false;
        }
        if(findById(category.getCategoryId())==null){
            log.info("你输入的ID“{}”不存在，请输入正确的ID", category.getCategoryId());
            return false;
        }
        category.setUpdateTime(LocalDateTime.now());
//        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Category::getCategoryId, category.getCategoryId());
//        int update = categoryMapper.update(category, lqw);
        int update = categoryMapper.updateById(category);
        return update>0;
    }

    @Override
    public List<Category> findAll() {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //降序排序
        lqw.orderByDesc(Category::getCreateTime);
        lqw.isNull(Category::getDeleted);
        return categoryMapper.selectList(lqw);
    }

    @Override
    public Category findById(String id) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getCategoryId, id);
        lqw.isNull(Category::getDeleted);
        return categoryMapper.selectOne(lqw);
//        return categoryMapper.selectById(id);
    }

    @Override
    public Category findByName(String name) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getCategoryName, name);
        lqw.isNull(Category::getDeleted);
        return categoryMapper.selectOne(lqw);
    }

    @Override
    public Boolean cancelDelete(String id) {
        return categoryMapper.cancelDelete(id);
    }


}
